package com.ruoyi.individual.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.book.domain.Book;
import com.ruoyi.book.domain.BorrowInfo;
import com.ruoyi.book.mapper.BookMapper;
import com.ruoyi.book.mapper.BorrowMapper;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.individual.domain.BorrowBook;
import com.ruoyi.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.individual.service.IMyBorrowService;

/**
 * 我的借阅Service业务层处理
 *
 * @author yaohui
 * @date 2024-05-08
 */
@Service
public class MyBorrowServiceImpl implements IMyBorrowService {
    @Autowired
    private BorrowMapper borrowMapper;
    @Autowired
    private SysUserMapper userMapper;
    @Autowired
    private BookMapper bookMapper;

    /**
     * 查询我的借阅
     *
     * @param id 借阅主键
     * @return 借阅详细
     */
    @Override
    public BorrowBook selectMyBorrowBookInfoById(Long id , String username) {
        SysUser sysUser = userMapper.selectUserByUserName(username);
        BorrowInfo borrowInfo = borrowMapper.selectById(id);
        Book book = bookMapper.selectBookByNum(borrowInfo.getBookNum());
        return BorrowBook.builder().pic(book.getPic())
                .bookNum(book.getNum())
                .name(book.getName())
                .description(book.getDescription())
                .borrowDate(borrowInfo.getBorrowDate())
                .returnDate(borrowInfo.getReturnDate())
                .state(borrowInfo.getState())
                .owner(book.getCreateBy())
                .phonenumber(sysUser.getPhonenumber())
                .location(sysUser.getLocation())
                .build();
    }

    /**
     * 查询我的借阅列表
     *
     * @param userName 账号
     * @return 我的借阅
     */
    @Override
    public List<Book> selectMyBorrowBookList(String userName) {
        SysUser sysUser = userMapper.selectUserByUserName(userName);
        List<BorrowInfo> borrowInfos = borrowMapper.selectBorrowListByUserId(sysUser.getUserId());
        ArrayList<Book> books = new ArrayList<>();
        // todo 优化
        for (BorrowInfo borrowInfo : borrowInfos) {
            String bookNum = borrowInfo.getBookNum();
            Book book = bookMapper.selectBorrowBookByNum(bookNum);
            books.add(book);
        }
        return books;
    }

    /**
     * 查询我的借阅详细信息
     *
     * @param borrowBook 查询条件
     * @param username   用户名
     * @return 详细信息
     */
    @Override
    public List<BorrowBook> selectMyBorrowBookInfoList(BorrowBook borrowBook, String username) {
        SysUser sysUser = userMapper.selectUserByUserName(username);
        List<Book> list = selectMyBorrowBookList(username);
        ArrayList<BorrowBook> borrowBooks = new ArrayList<>();
        for (Book book : list) {
            BorrowInfo borrowInfo = borrowMapper.selectByBookNum(book.getNum());
            BorrowBook build = BorrowBook.builder().pic(book.getPic())
                    .bookNum(book.getNum())
                    .name(book.getName())
                    .description(book.getDescription())
                    .borrowDate(borrowInfo.getBorrowDate())
                    .returnDate(borrowInfo.getReturnDate())
                    .state(borrowInfo.getState())
                    .owner(book.getCreateBy())
                    .phonenumber(sysUser.getPhonenumber())
                    .location(sysUser.getLocation())
                    .build();
            borrowBooks.add(build);
        }
        //todo borrowBook条件过滤查询没有完成
//        borrowBooks.stream().filter( e ->{e.getState() == e.getState()})
        return borrowBooks;
    }
}
